<!DOCTYPE html>
<?php
require_once('config.php');
$attempt = "1";
$testID = "1";
$last = false; // FOR COMPLETION STATUS
$_SESSION['finished'] = 'no'; // COMPLETION STATUS
$score = 0;  // FOR SCORE

function getStudentInfo($studentID)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM student WHERE studentID ='$studentID'";
	$result = mysqli_query($con,$query) or die ("Could not execute query");
	return mysqli_fetch_assoc($result);
}

function getStudent() {
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM student WHERE username = "
	. "'" . ($_SESSION['username']) . "'";
	$result = mysqli_query($con,$query) or die ("Could not execute query");
while ($row=mysqli_fetch_row($result))
    {
		$studentID = $row[0];     
		$_SESSION['studentID'] = $studentID;   
		$firstName = $row[4];     
		$_SESSION['firstName'] = $firstName;   
    }
}		


function getStudentResult($studentID)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM studentintest WHERE studentID ='$studentID'";
	$result = mysqli_query($con,$query) or die ("Could not execute query");
	return mysqli_fetch_assoc($result);
}



function getTestFeedback($studentID, $testID, $attempt)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM studentresponse WHERE studentID ='$studentID' AND testID = '$testID' AND attempt = '$attempt'";
	$result = mysqli_query($con,$query) or die ("Could not execute query");
	$nrows = mysqli_num_rows($result);
	for ($i=0;$i<$nrows;$i++)
		{
			$row = mysqli_fetch_assoc($result);
			extract($row);
		echo	
			"<ul>
			<li>Question: $questionID</li>\n
			<li>Test Feedback/Selected Answer: $selectedAnswer</li>\n
			<li>Test Feedback/Correct Answer: $correctAnswer</li>\n
			<li>Feedback: var to go here</li></ul>";	
		}
}

function getQuestion($currentQuestion)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM question WHERE questionID ='$currentQuestion'";
	$result = mysqli_query($con,$query) or die ("Could not execute query");
	return mysqli_fetch_assoc($result);
	extract($row);
	} 

	


function getIt($studentID, $attempt, $currentQuestion)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM studentresponse WHERE studentID ='$studentID' AND attempt = '$attempt' AND questionID ='$currentQuestion' AND id IN (SELECT MAX(id) FROM studentresponse GROUP BY studentID)";
	$result = mysqli_query($con,$query) or die ("Could not execute query");
	return mysqli_fetch_assoc($result);
	extract($row);
		}
		
function putStudentResponse($studentID, $attempt, $currentQuestion, $testID)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "INSERT INTO studentresponse (attempt, studentID, selectedAnswer, questionID, testID,  startTime)"
		. " VALUES ("
		. "'" . $attempt . "',"
		. "'" . $studentID . "',"
		. "'" . $_POST['questionOptions'] . "',"
		. "'" . $currentQuestion  . "',"
		. "'" . $testID  . "', NOW()"
		. ")";	
	$result = mysqli_query($con,$query) or die ("Could not execute query");
}

		
?>


